package com.ruoyi.performance.service.impl;

import com.ruoyi.performance.domain.PerfDirectScore;
import com.ruoyi.performance.mapper.PerfDirectScoreMapper;
import com.ruoyi.performance.service.PerfDirectScoreService;

import ch.qos.logback.classic.Logger;
import lombok.extern.slf4j.Slf4j;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

/**
 * @author YL
 * @date 2020/8/26 8:41
 * @description 绩效模板Service接口实现类
 */
@Service
@Slf4j
public class PerfDirectScoreServiceImpl implements PerfDirectScoreService {

    @Resource
    private PerfDirectScoreMapper perfDirectScoreMapper;

    /**
     * 查询直接打分
     *
     * @param ditemId 细项ID
     * @return 直接打分
     */
    @Override
    public PerfDirectScore selectPerfDirectScoreById(int ditemId) {
        return perfDirectScoreMapper.selectPerfDirectScoreById(ditemId);
    }

    /**
     * 新增直接打分
     *
     * @param perfDirectScore 直接打分
     * @return 结果
     */
    @Override
    public String insertPerfDirectScore(PerfDirectScore perfDirectScore) {
    	String result = "1";
    	int upperScore = perfDirectScore.getUpperScore();
    	int lowerScore = perfDirectScore.getLowerScore();
    	int score = perfDirectScore.getScore();
    	if (lowerScore >= upperScore)
    	{   
    		result = "下限值不能超过上限值，请修改后再发布！";
    		return result;
    	}
    	if (score >= upperScore)
    	{   
    		result = "默认值不能超过上限值，请修改后再发布！";
    		return result;
    	}
    	try {
    		perfDirectScoreMapper.insertPerfDirectScore(perfDirectScore);
		} catch (Exception e) {
			if (e instanceof DuplicateKeyException)
			{
				result = "细项分值，id+" + perfDirectScore.getDitemId() + "已存在，不允许重复添加";
				log.info(result);
				return result;
			}
			else {
				result = "新增细项分值，id：" + perfDirectScore.getDitemId() + "失败,请联系管理员！";
				log.error(result, e);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return result;
			}

		}
        return result;
    }

    /**
     * 修改直接打分
     *
     * @param perfDirectScore 直接打分
     * @return 结果
     */
    @Override
    public String updatePerfDirectScore(PerfDirectScore perfDirectScore) {
    	String result = "1";
    	int upperScore = perfDirectScore.getUpperScore();
    	int lowerScore = perfDirectScore.getLowerScore();
    	int score = perfDirectScore.getScore();
    	if (lowerScore >= upperScore)
    	{   
    		result = "下限值不能超过上限值，请修改后再发布！";
    		return result;
    	}
    	if (score >= upperScore)
    	{   
    		result = "默认值不能超过上限值，请修改后再发布！";
    		return result;
    	}
    	try {
			perfDirectScoreMapper.updatePerfDirectScore(perfDirectScore);
		} catch (Exception e) {
			if (e instanceof DuplicateKeyException)
			{
				result = "细项分值，id+" + perfDirectScore.getDitemId() + "已存在，不允许重复添加";
				log.info(result);
				return result;
			}
			else {
				result = "更新细项分值，id：" + perfDirectScore.getId() + "失败,请联系管理员！";
				log.error(result, e);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return result;
			}

		}
		return result;
    }
}
